// Google Video.js for PSPTube
// Made By JK108

function GoogleVideo_CheckURL( url, option )
{
	if (url.match("http://video.google.co.uk") == "http://video.google.co.uk"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a GoogleVideo URL
*/
function GoogleVideo_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/googleplayer.swf\?videoUrl\\x3d(.*?)\\x26/);
	var video_url = PSPTube.decodeURI(video_url[1]);
	return video_url;
}

function GoogleVideo_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;

	
	if(result.start < 10){
	var contents = GetContents( "http://video.google.co.uk/videosearch?hl=en&q=" + keyword.replace(/ /g, "%20") + "+site%3Avideo.google.com&__q=" + keyword.replace(/ /g, "%20") + "&num=10" );
	} else{
	var num = result.start - 1;
	var contents = GetContents( "http://video.google.co.uk/videosearch?hl=en&q=" + keyword.replace(/ /g, "%20") + "+site%3Avideo.google.com&__q=" + keyword.replace(/ /g, "%20") + "&num=10" + "&start=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("</strong> did not return any results.<br/><br/>") == "</strong> did not return any results.<br/><br/>"){result.total = 0; return result;}
	var total = contents.match(/\<\/b\> of about \<b\>(.*?)\<\/b\> for \<b\>/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<div class=rl-item>");

	if(pos != -1){
		contents = contents.substring(pos + 19, contents.length);
		//Thumbnail
		var thumb_url = contents.match(/\<img class\=thumbnail-img src\=(.*?)\>/);
		var thumb_url = thumb_url[1];
		//Get URL
		var url = contents.match(/\<a href\=(.*?)\s/);
		var url = "http://video.google.co.uk" + url[1];
		//Get Title
		pos = contents.indexOf(" target=_top>\n");
		var title = contents.substring(pos + 14, contents.length);
		pos = title.indexOf(" target=_top>\n");
		title = title.substring(pos + 14, contents.length);
		pos = title.indexOf("\n");
		title = removeUnidentifiedChars(ImproveText(title.substring(0, pos)));
		//Length
		var length = contents.match(/duration=(.*?)\s/);
		var length = parseInt(length[1]);
		//Description
		var description = contents.match(/<div class\=rl-snippet>\n(.*?)\n<\/div>/);
		var description = removeUnidentifiedChars(ImproveText(description[1]));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);

		info = new Object();
		info.ThumbnailURL = thumb_url;
		info.URL = url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end   = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != 10){result.total = result.end;}

	return result;
}

// Service metadata
var GoogleVideo = new Object();
GoogleVideo.Name          = "Google Video";		/* Service name */
GoogleVideo.Description   = "Google Video";		/* Service description */
GoogleVideo.SearchDesc    = "Google Video";		/* Service search screen description */
GoogleVideo.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
GoogleVideo.CheckURL      = GoogleVideo_CheckURL;	/* CheckURL function */
GoogleVideo.GetURL        = GoogleVideo_GetURL;		/* GetURL function */
GoogleVideo.Search        = GoogleVideo_Search;		/* Search function */

// add YouTube to site list
SiteList.push( GoogleVideo );
